package com.service;

import java.util.List;

import com.model.Admin;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

/**   
 *   
 * @author zhanghusong   
 */
public class AdminServiceImpl extends AbstractDateService implements
		AdminService {

	@Override
	public boolean deleteAdmin(Admin admin) {
		String sql = "delete from t_admin where id="+admin.getId();
		logger.info("[SQL] [method:deleteAdmin] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base, sql);
	}

	@Override
	public List<Admin> getAdmin() {
		String sql = "select id, loginname, password, name,isadmin,sp,channel, createtime, createuser, roleid, keyvalue from t_admin order by createtime desc";
		logger.info("[SQL] [method:getAdmin] ["+sql+"]");
		ModelGenerator<Admin> model = new ModelGenerator<Admin>(Admin.class);
		return model.generateList(base, sql.toString());
	}

	@Override
	public Admin getAdminById(Admin admin) {
		String sql = "select id, loginname, password, name,isadmin,sp,channel, createtime, createuser, roleid, keyvalue from t_admin " +
				" where id="+admin.getId()+" order by createtime desc";
		logger.info("[SQL] [method:getAdminById] ["+sql+"]");
		ModelGenerator<Admin> model = new ModelGenerator<Admin>(Admin.class);
		return model.generate(base, sql.toString());
	}

	@Override
	public int saveAdmin(Admin admin) {
		int tabid = getTableId("t_admin");
		String sql = "insert into t_admin (id, loginname, password, name,isadmin,sp,channel, createtime," +
				" createuser, roleid, keyvalue)values ("
		+tabid+", '"+admin.getLoginName()
		+"', '"+admin.getPassword()+"', '"+admin.getName()+"','"+admin.getIsAdmin()+"','"+admin.getSp()+"','"+admin.getChannel()+"', now(), '"+admin.getCreateUser()+"', "+admin.getRoleId()+", '"+admin.getKeyValue()+"')";
		logger.info("[SQL] [method:saveAdmin] ["+sql+"]");
		ConnectionManager.getInstance().execute(base, sql);
		return tabid;
	}

	@Override
	public boolean updateAdmin(Admin admin) {
		String sql = "update t_admin set password = '"+admin.getPassword()+"',name = '"+admin.getName()
		+"',isadmin='"+admin.getIsAdmin()+"',sp='"+admin.getSp()+"',channel='"+admin.getChannel()+"',roleid ="+admin.getRoleId()+",keyvalue = '"+admin.getKeyValue()+"' where id ="+admin.getId();
		logger.info("[SQL] [method:updateAdmin] ["+sql+"]");
		return ConnectionManager.getInstance().execute(base, sql);
	}

	@Override
	public List<Admin> getAdminByLoginName(Admin admin) {
		String sql = "select id, loginname, password, name,isadmin,sp,channel, createtime, createuser, roleid, keyvalue from t_admin " +
				" where loginname='"+admin.getLoginName()+"' order by createtime desc";
		logger.info("[SQL] [method:getAdminByLoginName] ["+sql+"]");
		ModelGenerator<Admin> model = new ModelGenerator<Admin>(Admin.class);
		return model.generateList(base, sql.toString());
	}

	@Override
	public List<Admin> getAdminByKeyWord(String keyWord) {
		String sql = "select id, loginname, password, name,isadmin,sp,channel, createtime, createuser, roleid, keyvalue from t_admin " +
				" where instr(loginname,'"+keyWord+"')>0 or instr(name,'"+keyWord+"')>0 order by createtime desc";
		logger.info("[SQL] [method:getAdmin] ["+sql+"]");
		ModelGenerator<Admin> model = new ModelGenerator<Admin>(Admin.class);
		return model.generateList(base, sql.toString());
	}


}
